{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Longest Sequence with Two Unique Numbers\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/BliVzh996PHOtghAC_POEw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a sequence of numbers, find the longest sequence that contains only 2 unique numbers."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input: [1, 3, 5, 3, 1, 3, 1, 5]\n",
    "Output: 4\n",
    "```\n",
    "\n",
    "The longest sequence that contains just 2 unique numbers is [3, 1, 3, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def long_seq(sequence):\n",
    "    sub_seq = 0\n",
    "    sub_set = 0\n",
    "    \n",
    "    # Check every subsequence of the raw sequence.\n",
    "    for i in range(len(sequence)):\n",
    "        for j in range(i + 1, len(sequence)):\n",
    "            # Get the subsequence.\n",
    "            temp_seq = sequence[i:j]\n",
    "            # Get all unique numbers in the subsequence.\n",
    "            temp_set = set(temp_seq)\n",
    "            # Check if the subsequence satisfy all conditions.\n",
    "            if len(temp_set) == 2 and len(temp_seq) > sub_seq:\n",
    "                sub_seq = len(temp_seq)\n",
    "                sub_set = len(temp_set)\n",
    "                pass\n",
    "            pass\n",
    "        pass\n",
    "    return sub_seq"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(long_seq([1, 3, 5, 3, 1, 3, 1, 5]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
